package cn.findviewbydream.evil.main.interceptor;

import cn.findviewbydream.evil.main.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/********************************************
 * 工程名：HebeuCS
 * 包名：cn.findviewbydream.evil.main.interceptor
 * 时间：16-10-31 下午7:10
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：登录的拦截器
 ********************************************/
public class LoginInterceptor implements HandlerInterceptor
{
    private final LoginService loginService;

    @Autowired
    public LoginInterceptor(LoginService loginService)
    {
        this.loginService = loginService;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception
    {
        HttpSession session = request.getSession();
        if (session.getAttribute("user") == null)
        {
            Cookie[] cookies = request.getCookies();
            loginService.getCookie(session, cookies);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception
    {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception
    {

    }
}
